﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Arizu.Repository;
using System.Data;
using Arizu.Entity;

namespace AiRiZu
{
    public partial class RoomList : System.Web.UI.Page
    {
        public string RoomHtml = "";

        public string PageHtml = "";

        public string str = "";

        public string AreaHtml = "";

        public string CityHtml = "";

        public int SearchResCount = 0;

        public List<string> SpecificRoomID = new List<string>();
        public List<string> SpecificRoomImg = new List<string>();
        public List<string> SpecificRoomTitle = new List<string>();
        public List<string> SpecificRoomCity = new List<string>();
        public List<string> SpecificRoomReason = new List<string>();

        public string searchCityValue = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["searchCityValue"] == null)
            {
                searchCityValue = "北京";
            }
            else
            {
                searchCityValue = Request.QueryString["searchCityValue"].ToString();
            }

            GetArea();

            GetRoomList();

            GetSpecificRoom();

            GetCity();
            //if (Session["searchCityValue"] == null)
            //{
            //    Session["searchCityValue"] = "北京";
            //}
            //AirizuRoom airizuRoom = AirizuRoom.CreateInstance();
            //AirizuPrice airizuPrice = AirizuPrice.CreateInstance();
            //AirizuComments arizuComment = AirizuComments.CreateInstance();

            //int currentPage = 0;
            //if (Request.QueryString["pageNum"] != null)
            //{
            //    currentPage = int.Parse(Request.QueryString["pageNum"].ToString());
            //}


            //DataTable dt = airizuRoom.GetRoomInfos(Session["searchCityValue"].ToString());


            //if (dt.Rows.Count > 0)
            //{
            //    StringBuilder roomloc = new StringBuilder();
            //    for (int i = currentPage * 8; i < ((currentPage + 1) * 8) && i < dt.Rows.Count; i++)
            //    {
            //        string roomAddr = dt.Rows[0]["Province"].ToString() + dt.Rows[0]["City"].ToString() + dt.Rows[0]["Area"].ToString() + dt.Rows[0]["Adressdesc"].ToString();
            //        DataTable dt1 = airizuPrice.GetPrice(long.Parse(dt.Rows[i]["SendRoomid"].ToString()));

            //        int DayPrice = 0;
            //        int originalPrice = 0;
            //        string beginTime = null;
            //        string endTime = null;
            //        int NumOfNight = 0;
            //        if (dt1.Rows.Count > 0)
            //        {
            //            originalPrice = int.Parse(dt1.Rows[0]["DayPrice"].ToString());

            //            if (dt1.Rows[0]["CheckIn"] != null && dt1.Rows[0]["CheckOut"] != null)
            //            {
            //                beginTime = new DateTime(long.Parse(dt1.Rows[0]["CheckIn"].ToString())).ToString("MM-dd");
            //                endTime = new DateTime(long.Parse(dt1.Rows[0]["CheckOut"].ToString())).ToString("MM-dd");

            //                TimeSpan checkinTimeSpan = new TimeSpan(long.Parse(dt1.Rows[0]["CheckIn"].ToString()));
            //                TimeSpan checkoutTimeSpan = new TimeSpan(long.Parse(dt1.Rows[0]["CheckOut"].ToString()));

            //                TimeSpan span = checkoutTimeSpan.Subtract(checkinTimeSpan);
            //                NumOfNight = Convert.ToInt32(span.TotalDays) + 1;

            //                if (long.Parse(dt1.Rows[0]["CheckIn"].ToString()) < System.DateTime.Now.Ticks
            //                    && long.Parse(dt1.Rows[0]["CheckOut"].ToString()) > System.DateTime.Now.Ticks)
            //                {
            //                    DayPrice = int.Parse(dt1.Rows[0]["AdvancedPrice"].ToString());
            //                }
            //            }
            //        }

            //        int commentCount = 0;
            //        DataTable dbcm = arizuComment.GetRoomCommentCount(long.Parse(dt.Rows[i]["SendRoomid"].ToString()));
            //        if (dbcm.Rows.Count > 0)
            //        {
            //            commentCount = int.Parse(dbcm.Rows[0][0].ToString());
            //        }

            //        ImgSendRoom list = Main.CreateInstance().GetImg(int.Parse(dt.Rows[i]["SendRoomid"].ToString()));
            //        roomloc.Append(roomAddr).Append(",");

            //        RoomHtml += assmebleRoomHtml(
            //            dt.Rows[i]["RoomTitle"].ToString(), 
            //            "Roomimg/" + list.Image0,
            //            dt.Rows[i]["RecommendedReason"].ToString(), 
            //            roomAddr,
            //            commentCount,
            //            DayPrice,
            //            originalPrice,
            //            beginTime,
            //            endTime,
            //            NumOfNight,
            //            dt.Rows[i]["SendRoomid"].ToString());
            //    }
            //    str = roomloc.ToString();
            //    assemblePageHtml(dt.Rows.Count / 8);
            //}

        }

        private void GetCity()
        {
            AirizuRoom airizuRoom = AirizuRoom.CreateInstance();
            DataTable dt = airizuRoom.GetCity();

            if (dt.Rows.Count > 0)
            {
                StringBuilder sBuilder = new StringBuilder();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    sBuilder.Append("<a href=\"RoomList.aspx?searchCityValue=" + dt.Rows[i]["City"].ToString() + "\">");
                    sBuilder.Append(dt.Rows[i]["City"].ToString());
                    sBuilder.Append("</a>");
                    //CityHtml = "<a href=\"#\">北京</a><a href=\"#\">testtset</a><a href=\"#\">南京</a>";
                }

                CityHtml = sBuilder.ToString();
            }
        }

        private void GetSpecificRoom()
        {
            AirizuRoom airizuRoom = AirizuRoom.CreateInstance();
            DataTable dt = airizuRoom.GetSpecificRoom();

            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < 2; i++)
                {
                    ImgSendRoom list = Main.CreateInstance().GetImg(int.Parse(dt.Rows[i]["SendRoomid"].ToString()));
                    SpecificRoomImg.Add("Roomimg/" + list.Image0);

                    SpecificRoomID.Add(dt.Rows[i]["SendRoomid"].ToString());
                    SpecificRoomTitle.Add(dt.Rows[i]["RoomTitle"].ToString());
                    SpecificRoomCity.Add(dt.Rows[i]["City"].ToString());
                    SpecificRoomReason.Add(dt.Rows[i]["RecommendedReason"].ToString());
                }
            }
        }

        private void GetArea()
        {
            AirizuRoom airizuRoom = AirizuRoom.CreateInstance();

            DataTable dt = Main.CreateInstance().GetCityID(searchCityValue);
            string cityID = "";
            if (dt.Rows.Count > 0)
            {
                cityID = dt.Rows[0]["cityID"].ToString();
            }

            DataTable dt2 = Main.CreateInstance().GetAreaList(cityID);

            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                sBuilder.Append("<a href=\"RoomList.aspx?searchCityValue=" + dt2.Rows[i]["areaName"].ToString() + "\">");
                sBuilder.Append(dt2.Rows[i]["areaName"].ToString());
                sBuilder.Append("</a>");
            }
            AreaHtml = sBuilder.ToString();

            //Message.Alert(AreaHtml);
        }

        private void GetRoomList()
        {
            AirizuRoom airizuRoom = AirizuRoom.CreateInstance();
            AirizuPrice airizuPrice = AirizuPrice.CreateInstance();
            AirizuComments arizuComment = AirizuComments.CreateInstance();

            int currentPage = 0;
            if (Request.QueryString["pageNum"] != null)
            {
                currentPage = int.Parse(Request.QueryString["pageNum"].ToString());
            }


            DataTable dt = airizuRoom.GetRoomInfos(searchCityValue.ToString());


            if (dt.Rows.Count > 0)
            {
                SearchResCount = dt.Rows.Count;

                StringBuilder roomloc = new StringBuilder();
                for (int i = currentPage * 8; i < ((currentPage + 1) * 8) && i < dt.Rows.Count; i++)
                {
                    string roomAddr = dt.Rows[0]["Province"].ToString() + dt.Rows[0]["City"].ToString() + dt.Rows[0]["Area"].ToString() + dt.Rows[0]["Adressdesc"].ToString();
                    DataTable dt1 = airizuPrice.GetPrice(long.Parse(dt.Rows[i]["SendRoomid"].ToString()));

                    int DayPrice = 0;
                    int originalPrice = 0;
                    string beginTime = null;
                    string endTime = null;
                    int NumOfNight = 0;
                    if (dt1.Rows.Count > 0)
                    {
                        originalPrice = int.Parse(dt1.Rows[0]["DayPrice"].ToString());

                        if (dt1.Rows[0]["CheckIn"] != null && dt1.Rows[0]["CheckOut"] != null && !string.IsNullOrEmpty(dt1.Rows[0]["CheckIn"].ToString()) && !string.IsNullOrEmpty(dt1.Rows[0]["CheckOut"].ToString()))
                        {
                            beginTime = new DateTime(long.Parse(dt1.Rows[0]["CheckIn"].ToString())).ToString("MM-dd");
                            endTime = new DateTime(long.Parse(dt1.Rows[0]["CheckOut"].ToString())).ToString("MM-dd");

                            TimeSpan checkinTimeSpan = new TimeSpan(long.Parse(dt1.Rows[0]["CheckIn"].ToString()));
                            TimeSpan checkoutTimeSpan = new TimeSpan(long.Parse(dt1.Rows[0]["CheckOut"].ToString()));

                            TimeSpan span = checkoutTimeSpan.Subtract(checkinTimeSpan);
                            NumOfNight = Convert.ToInt32(span.TotalDays) + 1;

                            if (long.Parse(dt1.Rows[0]["CheckIn"].ToString()) < System.DateTime.Now.Ticks
                                && long.Parse(dt1.Rows[0]["CheckOut"].ToString()) > System.DateTime.Now.Ticks)
                            {
                                DayPrice = int.Parse(dt1.Rows[0]["AdvancedPrice"].ToString());
                            }
                        }
                    }

                    int commentCount = 0;
                    DataTable dbcm = arizuComment.GetRoomCommentCount(long.Parse(dt.Rows[i]["SendRoomid"].ToString()));
                    if (dbcm.Rows.Count > 0)
                    {
                        commentCount = int.Parse(dbcm.Rows[0][0].ToString());
                    }

                    int indentCount = 0;
                    DataTable dbIndent = AirizuIndent.CreateInstance().GetIndentCount(dt.Rows[i]["SendRoomid"].ToString());
                    if (dbIndent.Rows.Count > 0)
                    {
                        indentCount = int.Parse(dbIndent.Rows[0][0].ToString());
                    }

                    ImgSendRoom list = Main.CreateInstance().GetImg(int.Parse(dt.Rows[i]["SendRoomid"].ToString()));
                    roomloc.Append(roomAddr).Append(",");

                    RoomHtml += assmebleRoomHtml(
                        dt.Rows[i]["RoomTitle"].ToString(),
                        "Roomimg/" + list.Image0,
                        dt.Rows[i]["RecommendedReason"].ToString(),
                        roomAddr,
                        commentCount,
                        DayPrice,
                        originalPrice,
                        beginTime,
                        endTime,
                        NumOfNight,
                        dt.Rows[i]["SendRoomid"].ToString(),
                        i % 8 + 1,
                    indentCount);
                }

                str = roomloc.ToString();

                assemblePageHtml(dt.Rows.Count / 8);

            }
        }
        //<span><a href=\"#\">加入收藏</a></span>
        private string assmebleRoomHtml1(string roomName, string roomPicUrl, string roomDescLong, string roomLocation,
            int countOfComment, int price, int originalPrice, string startTime, string endTime, int totleDays, string roomID)
        {
            StringBuilder sBuilder = new StringBuilder();
            sBuilder.Append("<li>").Append("<div class=\"m3_Result_img\"><div class=\"img_num1\">");
            sBuilder.Append(roomName);
            sBuilder.Append("</div><em><a href=\"Room.aspx?sendroomid=").Append(roomID).Append("\">");
            sBuilder.Append("<img src=\"");
            sBuilder.Append(roomPicUrl);
            sBuilder.Append("\" /></a></em></div><div class=\"m3_Result_text\">");
            sBuilder.Append("<div><strong><a href=\"Room.aspx?sendroomid=").Append(roomID).Append("\">");
            sBuilder.Append(roomName);
            sBuilder.Append("</a></strong></div><div><span>");
            sBuilder.Append(roomLocation);
            sBuilder.Append("</span></div><p>");
            sBuilder.Append(roomDescLong);
            sBuilder.Append("</p><div>");
            //sBuilder.Append("<p class=\"jia_1\"><strong>0</strong><span>感兴趣</span></p>");
            sBuilder.Append("<p class=\"jia_2\"><strong>");
            sBuilder.Append(countOfComment);
            sBuilder.Append("</strong><span>评论</span></p>");
            //sBuilder.Append("<p class=\"jia_3\"><span>距搜索目的地</span><strong>0.3km</strong></p>"); 
            sBuilder.Append("</div></div>");
            sBuilder.Append("<div class=\"m3_Result_num\">");
            if (price > 0)
            {
                sBuilder.Append("<div class=\"m3_Result_num_1\">");
                sBuilder.Append("<div class=\"tejia_bg\"></div>");
                sBuilder.Append("<h1 class=\"red_h1\">");
                sBuilder.Append(price);
                sBuilder.Append("</h1>");
                sBuilder.Append("<div>元/天</div>");
                sBuilder.Append("<strong>原价：");
                sBuilder.Append(originalPrice);
                sBuilder.Append("</strong></div>");
            }
            else
            {
                sBuilder.Append("<div class=\"m3_Result_num_1\">");
                sBuilder.Append("<h1>");
                sBuilder.Append(originalPrice);
                sBuilder.Append("</h1>");
                sBuilder.Append("<div>元/天</div>");

                sBuilder.Append("</div>");
            }
            if (!string.IsNullOrEmpty(endTime) && !string.IsNullOrEmpty(startTime))
            {
                sBuilder.Append("<p class=\"img_1\"><strong class=\"img_1_s\">");
                sBuilder.Append(startTime).Append(" 至 ").Append(endTime).Append(" 共 ").Append(totleDays).Append(" 天</strong></p>");
            }

            sBuilder.Append("<p class=\"img_2\">");
            sBuilder.Append("<strong>整套</strong></p>");
            sBuilder.Append("<p class=\"img_3\">");
            sBuilder.Append("<strong>2人</strong></p>");
            sBuilder.Append("</div></li>");

            return sBuilder.ToString();
        }

        private string assmebleRoomHtml(string roomName, string roomPicUrl, string roomDescLong, string roomLocation,
    int countOfComment, int price, int originalPrice, string startTime, string endTime, int totleDays, string roomID, int order, int orderCount)
        {
            StringBuilder sBuilder = new StringBuilder();
            sBuilder.Append("<li>").Append("<div class=\"m3_Result_img\"><div class=\"img_num1\">");
            sBuilder.Append(order);
            sBuilder.Append("</div><em><a href=\"Room.aspx?sendroomid=").Append(roomID).Append("\">");
            sBuilder.Append("<img src=\"");
            sBuilder.Append(roomPicUrl);
            sBuilder.Append("\" /></a></em> <span><a href=\"#\">加入收藏</a></span></div><div class=\"m3_Result_text\">");
            sBuilder.Append("<div><strong><a href=\"Room.aspx?sendroomid=").Append(roomID).Append("\">");
            sBuilder.Append(roomName);
            sBuilder.Append("</a></strong></div><div><span>");
            sBuilder.Append(roomLocation);
            sBuilder.Append("</span></div><p>");
            sBuilder.Append(roomDescLong);
            sBuilder.Append("</p><div>");
            //sBuilder.Append("<p class=\"jia_1\"><strong>0</strong><span>感兴趣</span></p>");
            sBuilder.Append("<p class=\"jia_2\"><strong>");
            sBuilder.Append(countOfComment);
            sBuilder.Append("</strong><span>评论</span></p>");
            //sBuilder.Append("<p class=\"jia_3\"><span>距搜索目的地</span><strong>0.3km</strong></p>");
            sBuilder.Append("<p class=\"jia_3\"><span>已被预订</span><span><strong>").Append(orderCount).Append("</strong></span><span>晚</span></p>");
            sBuilder.Append("</div></div>");
            sBuilder.Append("<div class=\"m3_Result_num\">");
            if (price > 0)
            {
                sBuilder.Append("<div class=\"m3_Result_num_1\">");
                sBuilder.Append("<div class=\"tejia_bg\"></div>");
                sBuilder.Append("<h1 class=\"red_h1\">");
                sBuilder.Append(price);
                sBuilder.Append("</h1>");
                sBuilder.Append("<div>元/天</div>");
                sBuilder.Append("<strong>原价：");
                sBuilder.Append(originalPrice);
                sBuilder.Append("</strong></div>");
            }
            else
            {
                sBuilder.Append("<div class=\"m3_Result_num_1\">");
                sBuilder.Append("<h1>");
                sBuilder.Append(originalPrice);
                sBuilder.Append("</h1>");
                sBuilder.Append("<div>元/天</div>");

                sBuilder.Append("</div>");
            }
            if (!string.IsNullOrEmpty(endTime) && !string.IsNullOrEmpty(startTime))
            {
                sBuilder.Append("<p class=\"img_1\"><strong class=\"img_1_s\">");
                sBuilder.Append(startTime).Append(" 至 ").Append(endTime).Append(" 共 ").Append(totleDays).Append(" 天</strong></p>");
            }

            sBuilder.Append("<p class=\"img_2\">");
            sBuilder.Append("<strong>整套</strong></p>");
            sBuilder.Append("<p class=\"img_3\">");
            sBuilder.Append("<strong>2人</strong></p>");
            sBuilder.Append("</div></li>");

            return sBuilder.ToString();
        }

        private void assemblePageHtml(int count)
        {
            int currentPage = 0;
            if (Request.QueryString["pageNum"] != null)
            {
                currentPage = int.Parse(Request.QueryString["pageNum"].ToString());
            }

            StringBuilder sBuilder = new StringBuilder();
            if (count > 0 && currentPage > 0)
            {
                sBuilder.Append("<a href=\"RoomList.aspx?pageNum=").Append(currentPage - 1).Append("\">").Append("上一页").Append("</a>");
            }

            for (int i = 0; i < count; i++)
            {
                sBuilder.Append("<a href=\"RoomList.aspx?pageNum=").Append(i).Append("\">").Append(i + 1).Append("</a>");
            }

            if (count > 0 && currentPage < count)
            {
                sBuilder.Append("<a href=\"RoomList.aspx?pageNum=").Append(currentPage + 1).Append("\">").Append("下一页").Append("</a>");
            }

            PageHtml = sBuilder.ToString();

        }
    }
}
